﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using StyleMVVM.DependencyInjection;
using StyleMVVM.Utilities;
#if !DOT_NET
using Windows.Foundation;
using Windows.Storage;
using Windows.Storage.Pickers;

#else
using Microsoft.Win32;

#endif

namespace StyleMVVM.View.Impl
{
	public sealed class FilePickerService : IFilePickerService
	{
		public static void RegisterExport(IDependencyInjectionContainer container)
		{
			container.Register(typeof(FilePickerService)).As(typeof(IFilePickerService)).
			          AndSharedPermenantly();
		}

#if NETFX_CORE
		public IAsyncOperation<IReadOnlyList<StorageFile>> PickMultipleFilesAsync(PickerLocationId location,
		                                                                          params string[] filterTypes)
		{
			return InternalPickMultipleFilesAsync(location, filterTypes).AsAsyncOperation();
		}

		public IAsyncOperation<StorageFile> PickFileAsync(PickerLocationId location, params string[] filterTypes)
		{
			return InternalPickFileAsync(location, filterTypes).AsAsyncOperation();
		}
#elif WINDOWS_PHONE
		public Task<IReadOnlyList<StorageFile>> PickMultipleFilesAsync(PickerLocationId location, params string[] filterTypes)
		{
			return InternalPickMultipleFilesAsync(location, filterTypes);
		}

		public Task<StorageFile> PickFileAsync(PickerLocationId location, params string[] filterTypes)
		{
			return InternalPickFileAsync(location, filterTypes);
		}
#elif DOT_NET
		public async Task<IReadOnlyList<string>> PickMultipleFilesAsync(PickerLocationId location, params string[] filterTypes)
		{
			OpenFileDialog openFileDialog = new OpenFileDialog();

			openFileDialog.InitialDirectory = TranslateLocation(location);

			openFileDialog.Multiselect = true;
			openFileDialog.ShowDialog();

			if (openFileDialog.FileNames != null)
			{
				return new List<string>(openFileDialog.FileNames);
			}

			return new List<string>();
		}

		public async Task<string> PickFileAsync(PickerLocationId location, params string[] filterTypes)
		{
			OpenFileDialog openFileDialog = new OpenFileDialog();

			openFileDialog.InitialDirectory = TranslateLocation(location);

			openFileDialog.Multiselect = false;
			openFileDialog.ShowDialog();

			return openFileDialog.FileName;
		}

		public async Task<string> PickSaveFileAsync(PickerLocationId location, params string[] filterTypes)
		{
			SaveFileDialog saveFileDialog = new SaveFileDialog();

			saveFileDialog.InitialDirectory = TranslateLocation(location);

			saveFileDialog.ShowDialog();

			return saveFileDialog.FileName;
		}

		private string TranslateLocation(PickerLocationId location)
		{
			switch (location)
			{
				case PickerLocationId.ComputerFolder:
					return Environment.GetFolderPath(Environment.SpecialFolder.MyComputer);
					break;
				case PickerLocationId.Desktop:
					return Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
					break;
				case PickerLocationId.DocumentsLibrary:
					return Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
					break;
				case PickerLocationId.Downloads:
					string pathUser = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
					return Path.Combine(pathUser, "Downloads");
					break;
				case PickerLocationId.HomeGroup:
					break;
				case PickerLocationId.MusicLibrary:
					return Environment.GetFolderPath(Environment.SpecialFolder.MyMusic);
					break;
				case PickerLocationId.PicturesLibrary:
					return Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
					break;
				case PickerLocationId.VideosLibrary:
					return Environment.GetFolderPath(Environment.SpecialFolder.MyVideos);
					break;
			}

			return null;
		}
#endif

#if DOT_NET

#else
		internal async Task<IReadOnlyList<StorageFile>> InternalPickMultipleFilesAsync(
			PickerLocationId location = PickerLocationId.DocumentsLibrary, params string[] filterTypes)
		{
			FileOpenPicker openPicker = new FileOpenPicker();

			openPicker.SuggestedStartLocation = location;

			if (filterTypes != null)
			{
				foreach (string filterType in filterTypes)
				{
					openPicker.FileTypeFilter.Add(filterType);
				}
			}

			return await openPicker.PickMultipleFilesAsync();
		}

		internal async Task<StorageFile> InternalPickFileAsync(PickerLocationId location = PickerLocationId.DocumentsLibrary,
		                                                       params string[] filterTypes)
		{
			FileOpenPicker openPicker = new FileOpenPicker();

			openPicker.SuggestedStartLocation = location;

			if (filterTypes != null)
			{
				foreach (string filterType in filterTypes)
				{
					openPicker.FileTypeFilter.Add(filterType);
				}
			}

			return await openPicker.PickSingleFileAsync();
		}
#endif
	}
}